package com.sniper.springmvc.utils;

import com.sniper.springmvc.freemarker.ViewProjectImport;
import com.sniper.springmvc.model.JyzxReceive;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map.Entry;

/**
 * 数据导出
 *
 * @author suzhen
 */
public class ExeclJyzxExportUtil extends ExeclBaseUtil {


    public ExeclJyzxExportUtil() {
    }


    /**
     * 数据导出
     *
     * @param fileExportPath
     * @param subjects
     * @throws IOException
     */
    public void export(String fileExportPath, List<JyzxReceive> subjects) throws IOException {

        if (fileExportPath.endsWith("xlsx")) {
            this.workbook = new XSSFWorkbook();
        } else {
            this.workbook = new HSSFWorkbook();
        }
        Sheet sheet = workbook.createSheet("数据导出");
        // 设置导出的头部
        setSheetHeader(sheet);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 导出的内容
        if (subjects != null && subjects.size() > 0) {
            int j = headStart;
            // 序号
            int number = 1;
            for (JyzxReceive subject : subjects) {
                int i = 0;
                Row row = sheet.createRow(j);
                for (Entry<String, String> entry : valueMapFields.entrySet()) {
                    switch (entry.getKey()) {
                        // 序号设置
                        case "id":
                            createCell(row, i, number, CellStyle.ALIGN_CENTER,
                                    CellStyle.VERTICAL_CENTER);
                            break;
                        // 特殊处理的时间
                        case "date":
                            createCell(row, i,
                                    dateFormat.format(subject.getDatetime()),
                                    CellStyle.ALIGN_CENTER,
                                    CellStyle.VERTICAL_CENTER);
                            break;
                        default:
                            try {
                                createCell(row, i, ViewProjectImport.getFiledValue(
                                        subject, entry.getKey()),
                                        CellStyle.ALIGN_CENTER,
                                        CellStyle.VERTICAL_CENTER);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            break;
                    }
                    i++;
                }
                number++;
                j++;
            }
        }

        fileExportPath = rootDir + fileExportPath;
        LOGGER.debug(fileExportPath);
        //System.out.println(fileExportPath);
        File file = new File(fileExportPath);
        File parent = file.getParentFile();
        if (!parent.isDirectory()) {
            parent.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(fileExportPath);
        workbook.write(out);
        workbook.close();
        out.close();
    }
}
